﻿#include "datebase.h"
#include "filemanage.h"
#include "systemfile.h"
#define CREATE_INFO_CARBIND "CREATE TABLE [car_bind] ([id] INTEGER(20),[code] varchar(30),[car_no] varchar(20),[stall_code] varchar(30),[status] varchar(20),begin_date varchar(30),[end_date] varchar(30),[created_at] TIMESTAMP(20),[updated_at] TIMESTAMP(20),CONSTRAINT [sqlite_autoindex_car_bind_1] PRIMARY KEY ([id]))"
#define CREATE_INFO_DISCOUNT "CREATE TABLE [Discount] ([id] INTEGER(10),[discount_key] varchar(20),[discount_value] varchar(20),[status] varchar(10),[created_at] TIMESTAMP(20),[updated_at] TIMESTAMP(20),CONSTRAINT [sqlite_autoindex_Discount_1] PRIMARY KEY ([id]))"
#define CREATE_INFO_DOWNTIME "CREATE TABLE [DownTime] ([id] INTEGER(10),[assistant_handler_down_discount_time] varchar(20),[assistant_handler_down_info_stall_time] varchar(20),[assistant_handler_down_info_vehicle_time] varchar(20),[assistant_handler_down_record_prepay_time] varchar(20),[assistant_handler_down_record_stall_vehicle_time] varchar(20),[assistant_andler_down_tempfee_time] varchar(20),[assistant_handler_in_out_record_download_time] varchar(20),[handler_down_discount_time] varchar(20),[handler_down_info_stall_time] varchar(20),[handler_down_info_vehicle_time] varchar(20),[handler_down_record_prepay_time] varchar(20),[handler_down_record_stall_vehicle_time] varchar(20),[handler_down_tempfee_time] varchar(20),[handler_in_out_record_download_time] varchar(20),[myid] varchar(20),CONSTRAINT [sqlite_autoindex_DownTime_1] PRIMARY KEY ([id]))"
#define CREATE_INFO_MONEY "CREATE TABLE [money] ([id] INTEGER PRIMARY KEY AUTOINCREMENT,[car_type_code] varchar(20),[car_type_name] varchar(20),[created_at] DATETIME(30),[fee_code] varchar(20),[fee_detail_code] varchar(20),[fee_name] varchar(20),[money] DECIMAL(20),[parked_max_time] INTEGER(20),[parked_min_time] INTEGER(20),[updated_at] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime')))"
#define CREATE_INFO_PAYMENT "CREATE TABLE [Payment] ([id] INTEGER(10),[actual] DECIMAL(20),[car_no] varchar(20),[cashier] varchar(10),[created_at] DATETIME(20),[discount] DECIMAL(20),[modife_flage] varchar(20),[pass_no] varchar(20),[pay_way] varchar(20),[payee_type] varchar(20),[prepay_id] varchar(20),[receivable] DECIMAL(20),[status] varchar(20),[terminal_type] varchar(20),[updated_at] TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime')),CONSTRAINT [sqlite_autoindex_Payment_1] PRIMARY KEY ([id]))"
//DECIMAL
#define CREATE_RECORD_IN_OUT "CREATE TABLE [record_in_out] ([id] INTEGER PRIMARY KEY AUTOINCREMENT,[actual_money] DECIMAL(20),[car_no] varchar(20),[car_type] varchar(20),[discount] DECIMAL(20),[in_image] varchar(30),[in_time] DATETIME(30),[in_user] varchar(30),[modife_flage] INTEGER(10),[out_image] varchar(30),[out_time] DATETIME(30),[out_user] varchar(30),[pass_no] varchar(30),[pay_way] varchar(10),[receivable] DECIMAL(20),[stall] varchar(10),[stall_time] varchar(10),[status] varchar(20),[tatal_actual] DECIMAL(20),[tatal_receiv] DECIMAL(20),[update_time] TimeStamp NOT NULL DEFAULT (datetime('now','localtime')),[updated_controller_sn] varchar(30))"
#define CREATE_RECORD_STALL_VEHICLE "CREATE TABLE [record_stall_vehicle] ([id] INTEGER PRIMARY KEY AUTOINCREMENT,[allow_count] INTEGER(10),[allow_park_time] INTEGER(10),[balance] DECIMAL(20),[car_no] varchar(20),[car_type] varchar(20),[codeId] varchar(20),[created_at] DATETIME(30) ,[fee_type] varchar(20),[person_address] varchar(20),[person_name] varchar(20),[person_tel] varchar(20),[start_date] DATETIME(30),[status] varchar(20),[stop_date] DATETIME(30),[updated_at] TIMESTAMP(20),[vehicle_type] varchar(20))"

//#define CREATE_TEMP_VEHICLE "CREATE TABLE [temp_vehicle] ([code] VARCHAR(30),[car_no] VARCHAR(20),[create_time] DATETIME(20),[dir] VARCHAR(20))"
#define CREATE_INFO_USER "CREATE TABLE [user] ([id] INTEGER PRIMARY KEY AUTOINCREMENT,[password] varchar(20),[type] varchar(20),[userName] varchar(20))"
#define CREATE_VEHICLE_SEAT "CREATE TABLE IF NOT EXISTS  vehicle_seat(id INTEGER PRIMARY KEY AUTOINCREMENT,area_code varchar(30),print_code VARCHAR(20),stall_code VARCHAR(20),status VARCHAR(20),created_at TimeStamp NOT NULL DEFAULT (datetime('now','localtime')),[updated_at] TimeStamp NOT NULL DEFAULT (datetime('1970-01-01 01:00:00')))"
//#define CREAT_CAR_BIND "CREATE TABLE IF NOT EXISTS  car_bind(id INTEGER PRIMARY KEY ,code varchar(30),car_no varchar(30),stall_code VARCHAR(30),status VARCHAR(30) ,begin_date varchar(30),end_date varchar(30),[created_at] TimeStamp Not NULL DEFAULT (datetime('1970-01-01 01:00:00')),[updated_at] TimeStamp NOT NULL DEFAULT (datetime('1970-01-01 01:00:00')))"
//#define CREAT_INFOUPDATE_TIME "CREATE TABLE IF NOT EXISTS  update_last(id INTEGER PRIMARY KEY ,[updated_at] TimeStamp NOT NULL DEFAULT (datetime('1970-01-01 01:00:00')))"

#define postFindDly 60*10
datebase::datebase()
{
    //QString sqlite_path="./";
    QString sqlite_path=sysInfo->sqdbPath;
    sqlite_db = QSqlDatabase::addDatabase("QSQLITE","one");
    QString dbname=("tenement_passing_manager_new.db");
    //QString dbname=QString("tenement_passing_manager.db");
    sqlite_db.setDatabaseName(dbname);
    if(!QFile::exists(dbname))
    {
        if(!sqlite_db.open())
        {
            qDebug()<<"Unable to open sqlite database";
            return;
        }
        QStringList create_cmd;
        create_cmd.append(CREATE_INFO_CARBIND);
        create_cmd.append(CREATE_INFO_DISCOUNT);
        create_cmd.append(CREATE_INFO_DOWNTIME);
        create_cmd.append(CREATE_INFO_MONEY);
        create_cmd.append(CREATE_INFO_PAYMENT);

        create_cmd.append(CREATE_RECORD_IN_OUT);
        create_cmd.append(CREATE_RECORD_STALL_VEHICLE);

        create_cmd.append(CREATE_INFO_USER);
        create_cmd.append(CREATE_VEHICLE_SEAT);

        //create_cmd.append(CREAT_CAR_BIND);
        //create_cmd.append(CREATE_MONEY);
        QSqlQuery *query = new QSqlQuery(sqlite_db);
        foreach(QString cmd,create_cmd)
        {
            if(!query->exec(cmd))
            {
                qDebug()<<"build sqlite error"<<query->lastError().text();
                return;
            }else{
               qDebug()<<"build sqlite success";
            }
        }
        setFeeData(&sqlite_db,"临时车",0,0);
        setUserData(&sqlite_db,"管理员","123456","system");
        setUserData(&sqlite_db,"操作员","123456","common");
    }else{
        if(!sqlite_db.open())
        {
            qDebug()<<"Unable to open sqlite database";
        }
        else
        {
            qDebug()<<"open sqlite database sucsess";
        }
    }


    sqlite_db_bei =QSqlDatabase::addDatabase("QSQLITE","two");
    QString dbnamebei=("tenement_passing_manager_new_bei.db");
    sqlite_db_bei.setDatabaseName(dbnamebei);
    if(!QFile::exists(dbnamebei))
    {
        if(!sqlite_db_bei.open())
        {
            qDebug()<<"Unable to open sqlitebei database";
            return;
        }
        QStringList create_cmd;
        create_cmd.append(CREATE_INFO_CARBIND);
        create_cmd.append(CREATE_INFO_DISCOUNT);
        create_cmd.append(CREATE_INFO_DOWNTIME);
        create_cmd.append(CREATE_INFO_MONEY);
        create_cmd.append(CREATE_INFO_PAYMENT);

        create_cmd.append(CREATE_RECORD_IN_OUT);
        create_cmd.append(CREATE_RECORD_STALL_VEHICLE);

        create_cmd.append(CREATE_INFO_USER);
        create_cmd.append(CREATE_VEHICLE_SEAT);
        QSqlQuery *query = new QSqlQuery(sqlite_db_bei);
        foreach(QString cmd,create_cmd)
        {
            if(!query->exec(cmd))
            {
                qDebug()<<"build sqlitebei error:"<<query->lastError().text();
                return;
            }else{
                qDebug()<<"build sqliteBei sucsess";
            }
        }

    }else{
        if(!sqlite_db_bei.open())
        {
            qDebug()<<"Unable to open sqliteBei";
        }
        else
        qDebug()<<"open sqlite sqliteBei sucsess";
    }
}

void datebase::hand_delete(QSqlDatabase *db)
{
    QSqlQuery query(*db);
    query.clear();
    query.exec(QObject::tr("delete from hand"));
    qDebug()<<"delete sqlite success";
}

void datebase::hand_insert(QSqlDatabase *db,QString id,QString name)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare("insert into hand(id,reason) values(?,?)");
    query.bindValue(0,id);
    query.bindValue(1,name);
    query.exec();
    query.next();
}

int datebase::CarSeatSearch(QSqlDatabase *db,QString per_num)
{
    QSqlQuery query(*db);
    int seat;
    query.clear();
    query.prepare(QObject::tr("select park_seat_num from vehicle_seat where house_num='%1'").arg(per_num));
    query.exec();
    int ret=query.next();
    if(ret==1)
    {
        seat=query.value(0).toInt();
        query.clear();
    }else{
        seat=-1;
    }
    query.clear();
    qDebug()<<"bool datebase::CarSeatSearch"<<ret<<"back seat num="<<seat;
    return seat;
}

bool datebase::CarSeatIn(QSqlDatabase *db,QString per_num,int carNum,QString car_no)
{
    //  QSqlQuery query(*db);
    QSqlQuery query_seat(*db);
    int nes,inSeat;
    QString str=QObject::tr("select count(*) from record_in_out where person_address='%1' and status='已入' and car_no != '%2'").arg(per_num).arg(car_no);
    query_seat.clear();
    query_seat.prepare(str);
    query_seat.exec();
    nes=query_seat.next();
    if(nes==1)
    {
        inSeat= query_seat.value(0).toInt();
        qDebug()<<str<<"入场车辆数In Num"<<inSeat<<"车位数All Num"<<carNum;
        if(inSeat >= carNum)
        {
            qDebug()<<"CarSeatIn return false";
            return false;
        }else{
            qDebug()<<"CarSeatIn return true";
            return true;
        }
    }else{
        qDebug()<<"CarSeatIn return true";
        return true;
    }
}

/////////car_check need changed
bool datebase::car_chek(QSqlDatabase *db,ticketCheck_t *checkInfo,QString vehicle_flag,QString vehicle_no)
{
    QSqlQuery query(*db);
    query.clear();
    QString str;
    QString car_no;
    if(vehicle_no=="无牌")
    {
        checkInfo->car_type="临时车";
        checkInfo->person_address="";
        return false;
    }else{
        if(vehicle_flag=="1")  //校验车号所有位数
        {
            str=QObject::tr("select car_no,id,stop_date,fee_type,person_address  from record_stall_vehicle where car_no='%1'").arg(vehicle_no);
            query.prepare(str);
        }
        else if(vehicle_flag=="0")  //校验车号后6位
        {
           car_no ='%'+vehicle_no.right(5)+'%';
            str=QObject::tr("select car_no,id,stop_date,fee_type,person_address from record_stall_vehicle where car_no like :inputName");
            query.prepare(str);
            query.bindValue(":inputName",car_no);
        }
    }
    query.exec();
    int ret = query.next();
    qDebug()<<"car_chek*****"<<car_no<<ret<<str;
    if(ret==1) //固定车
    {
        QString car_ty,vehicle_No_check;
        vehicle_No_check=query.value(2).toString();
        checkInfo->endTime=query.value(2).toString().left(10);
        car_ty=query.value(3).toString();
        checkInfo->car_type=car_ty;
        if(vehicle_No_check.isEmpty())
        {
            checkInfo->car_type="临时车";
            qDebug()<<vehicle_no<<"**car_chek false无车号信息**";
            return false;
        }
        //checkInfo->person_address=query.value(4).toString();
        QDate endDate=QDate::fromString(checkInfo->endTime,"yyyy-MM-dd");
        QDate time = QDate::currentDate();
        QString cuttime = time.toString("yyyy-MM-dd");
        int day_section=time.daysTo(endDate);
        qDebug()<<vehicle_no<<"结束日期"<<checkInfo->endTime<<"当前时间"<<cuttime<<"到期天数"<<day_section;
        if(day_section >= 0)
        {
            checkInfo->ticketReturn="nomal";
            checkInfo->dayAuto=time.daysTo(endDate);
            qDebug()<<vehicle_no<<"**car_chek true **"<<checkInfo->ticketReturn<<"cuttime:"<<cuttime<<"endTime:"<<checkInfo->endTime<<checkInfo->dayAuto;
            return true;
        }else{
            checkInfo->ticketReturn="不在有效期";
            checkInfo->dayAuto=time.daysTo(endDate);
            qDebug()<<vehicle_no<<"**car_chek true--不在有效期bu zai you xiao**"<<checkInfo->ticketReturn<<"cuttime:"<<cuttime<<"endTime:"<<checkInfo->endTime;
            return true;
        }
    }else{
        checkInfo->car_type="临时车";
        checkInfo->person_address="";
        qDebug()<<vehicle_no<<"**car_chek11 false无车号信息**";
        return false;
    }
}

bool datebase::car_chek(QSqlDatabase *db,car_Check *carInfo,QString vehicle_flag,QString vehicle_no)
{
    QSqlQuery query(*db);
    query.clear();
    QString str;
    if(vehicle_flag=="1")  //校验车号所有位数
    {
        str=QObject::tr("select car_no,stop_date,status from record_stall_vehicle where car_no='%1'").arg(vehicle_no);
        query.prepare(str);
    }
    else if(vehicle_flag=="0")  //校验车号后6位
    {
        QString car_no='%'+vehicle_no.right(5)+'%';
        str=QObject::tr("select car_no,stop_date,status,car_type from record_stall_vehicle where car_no like :inputName");
        query.prepare(str);
        query.bindValue(":inputName",car_no);
    }
    query.exec();
    int ret = query.next();
    if(ret==1) //固定车
    { QString car_ty;
        carInfo->stop_date=query.value(1).toString().left(10);
        car_ty=query.value(3).toString();
        carInfo->car_type=car_ty;
        if(car_ty.isEmpty())
        {
            carInfo->car_type="固定车";
        }
        QDate endDate=QDate::fromString(carInfo->stop_date,"yyyy-MM-dd");
        QDate time = QDate::currentDate();
        QString cuttime = time.toString("yyyy-MM-dd");
        int day_section=time.daysTo(endDate);
        qDebug()<<vehicle_no<<"结束日期"<<carInfo->stop_date<<"当前时间"<<cuttime<<"到期天数"<<day_section;
        if(day_section >= 0)
        {
            carInfo->carReturn="nomal";
            carInfo->dayAuto=time.daysTo(endDate);
            qDebug()<<vehicle_no<<"**car_chek true**"<<carInfo->carReturn<<"cuttime:"<<cuttime<<"endTime:"<<carInfo->stop_date<<carInfo->dayAuto;
            return true;
        }else{
            carInfo->carReturn="不在有效期";
            carInfo->dayAuto=time.daysTo(endDate);
            qDebug()<<vehicle_no<<"**car_chek true--不在有效期bu zai you xiao**"<<carInfo->carReturn<<"cuttime:"<<cuttime<<"endTime:"<<carInfo->stop_date;
            return true;
        }
    }else{
        carInfo->car_type="临时车";
        //checkInfo->person_address="";
        qDebug()<<vehicle_no<<"**car_chek11 false无车号信息**";
        return false;
    }
}

bool datebase::car_outchek(QSqlDatabase *db,ticketCheck_t *checkInfo,QString vehicle_flag,QString vehicle_no)
{
    QSqlQuery query(*db);
    query.clear();
    if(vehicle_flag=="1")  //校验车号所有位数
    {
        query.prepare(QObject::tr("select car_no,id,stop_date,car_type,person_address  from record_stall_vehicle where car_no='%1'").arg(vehicle_no));
    }
    else if(vehicle_flag=="0")  //校验车号后6位
    {
        QString car_no='%'+vehicle_no.right(5)+'%';
        query.prepare(QObject::tr("select car_no,id,stop_date,car_type,person_address from record_stall_vehicle where car_no like :inputName"));
        query.bindValue(":inputName",car_no);
    }
    query.exec();
    int ret = query.next();
    if(ret==1) //固定车
    {
        //checkInfo->ticketReturn=query.value(0).toString();
        QString car_ty;
        checkInfo->endTime=query.value(2).toString();
        //checkInfo->endTime=query.value(2).toString().left(10);
        car_ty=query.value(3).toString();
        checkInfo->car_type=car_ty;
        if(car_ty=="特殊车")
        {
            checkInfo->car_type="特殊车";
            qDebug()<<vehicle_no<<"出口car_chek true false--特殊车te shu che";
            return false;
        }else{
            checkInfo->car_type="固定车";
            checkInfo->person_address=query.value(4).toString();
            QDate endDate=QDate::fromString(checkInfo->endTime.left(10),"yyyy-MM-dd");
            QDate time = QDate::currentDate();
            QString cuttime = time.toString("yyyy-MM-dd");
            int day_section=time.daysTo(endDate);
            if(day_section >= 0)
            {
                if(checkInfo->person_address.isEmpty())
                {
                    checkInfo->ticketReturn="nomal";
                    checkInfo->dayAuto=time.daysTo(endDate);
                    qDebug()<<vehicle_no<<"出口car_chek true-- nomal"<<checkInfo->ticketReturn<<"cuttime:"<<cuttime<<"endTime:"<<checkInfo->endTime<<checkInfo->dayAuto;
                    return true;
                }else{
                    checkInfo->ticketReturn="一路平安请出场 ";

                    checkInfo->dayAuto=time.daysTo(endDate);
                    qDebug()<<vehicle_no<<"出口car_chek true-- 一路平安"<<checkInfo->ticketReturn<<"cuttime:"<<cuttime<<"endTime:"<<checkInfo->endTime<<checkInfo->dayAuto;
                    return true;
                }
            }else{
                checkInfo->ticketReturn="不在有效期";
                checkInfo->dayAuto=time.daysTo(endDate);
                qDebug()<<vehicle_no<<"出口car_chek true--不在有效期"<<checkInfo->ticketReturn<<"cuttime:"<<cuttime<<"endTime:"<<checkInfo->endTime<<checkInfo->dayAuto;
                return true;
            }
        }
    }else{
        qDebug()<<vehicle_no<<"出口car_chek true false无车号信息";
        return false;
    }
}

bool datebase::writeCarLinInLog(QSqlDatabase *db,QString car_no,QString picname,QString car_type,QString per_address,QString statu)
{
    QDateTime time = QDateTime::currentDateTime();
    QString crnttime = time.toString("yyyy-MM-dd hh:mm:ss");
    QString pound_no = sysInfo->galleryNumber+time.toString("yyMMddhhmmsszzz");
    qDebug()<<"in--------------------------";
    QSqlQuery query(*db);
    QString str;
    if(statu==QString("已入"))
    {
        //check QSqlDatabase 是否有已入记录，update / insert
        str="insert into record_in_out(pass_no,card_type,in_time,in_door,car_no,status,created_at,out_time,out_door,part_time,in_image,person_address,in_time_var,created_at_var,stall_code) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    }
    query.clear();
    query.prepare(str);
    query.bindValue(0,pound_no);//pass_no
    query.bindValue(1,car_type);//card_type
    query.bindValue(2,crnttime);//in_time
    query.bindValue(3,sysInfo->galleryNumber);//in_door
    query.bindValue(4,car_no);//car_no
    query.bindValue(5,statu);//status
    query.bindValue(6,crnttime);//created_at
    query.bindValue(7,"");//out_time
    query.bindValue(8,"");//out_door
    query.bindValue(9,"");//part_time
    query.bindValue(10,picname);//in_image
    query.bindValue(11,per_address);//person_address
    query.bindValue(12,crnttime);//in_time_var
    query.bindValue(13,crnttime);//created_at_var
    query.bindValue(14,sysInfo->car_stall);//stall_code  占用车位号，车位表主键
    bool guet = query.exec();
    if(guet)
    {
        qDebug()<<"writeCarInLog"<<car_no<<picname<<car_type<<per_address<<statu<<sysInfo->galleryNumber;
        return true;
    }else{
        qDebug()<<"writeCarInLog return fasle"<<query.lastError().text();
        return false;
    }
}

bool datebase::writeCarInLog(QSqlDatabase *db,QString car_no,QString picname,QString car_type,QString stall,QString statu,QString in_user)
{
    //bool mes,res;
    QDateTime time = QDateTime::currentDateTime();
    QString crnttime = time.toString("yyyy-MM-dd hh:mm:ss");
    QString pound_no = sysInfo->mac+time.toString("yyyyMMddhhmmsszzz");
    QSqlQuery query(*db);
    QString str;
//    if(statu==QString("已入"))
//    {
//        //check QSqlDatabase 是否有已入记录，update / insert
//        mes=query.exec(QString("select * from record_in_out where car_no='%1' and status='%2' and modife_flage=0").arg(car_no).arg(statu));
//        if(mes)
//        {
//            query.clear();
//            query.exec(QString("update record_in_out set update_time='%1',out_time='%2',status='%3',modife_flage=1 where car_no='%4' and status='%5'").arg(crnttime).arg(crnttime).arg("跟出").arg(car_no).arg(statu));
//            res=query.next();
//            if(res){
//                qDebug()<<"update record_in_out where status:跟出 car_no: "<<car_no<<"is ok.";
//            }
//        }
//    }
    str="insert into record_in_out(pass_no,update_time,car_type,in_time,in_user,car_no,status,out_time,out_user,stall_time,in_image,stall,modife_flage) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    query.clear();
    query.prepare(str);
    query.bindValue(0,pound_no);//pass_no
    query.bindValue(1,crnttime);//update_at
    query.bindValue(2,car_type);//car_type
    query.bindValue(3,crnttime);//in_time
    query.bindValue(4,in_user);//in_user
    query.bindValue(5,car_no);//car_no
    query.bindValue(6,statu);//status
    query.bindValue(7,"");//out_time
    query.bindValue(8,"");//out_user
    query.bindValue(9,"");//stall_time
    query.bindValue(10,picname);//in_image
    query.bindValue(11,stall);//stall 占用车位号，车位表主键
    query.bindValue(12,QString::number(0));//stall 占用车位号，车位表主键
    bool guet = query.exec();
    if(guet)
    {
        qDebug()<<"writeCarInLog_new"<<car_no<<picname<<car_type<<stall<<statu<<in_user;
        return true;
    }else{
        qDebug()<<"writeCarInLog return fasle"<<query.lastError().text();
        return false;
    }
}

QString datebase::check_stall_car(QSqlDatabase *db,QString carNum,QStringList stall_code)
{
    QSqlQuery query(*db);
    QString str;
   // bool back=true;
    for(int i=0;i<stall_code.count();i++)
    {
        str=QObject::tr("select car_no from record_in_out where status='%1' and stall_code='%2'").arg("已入").arg(stall_code.at(i));
        query.clear();
        query.prepare(str);
        query.exec();
        if(query.next())
        {
            if(carNum==query.value(0).toString())
            {//chang status 已出
                query.clear();
                query.exec(QObject::tr("update status where car_no='%1' and status='%2'").arg(carNum).arg("跟出"));
                //use_code=stall_code.at(i);
                //return false;
                return stall_code.at(i);
            }
            else if(i==stall_code.count())
            {
                //back=true;
                return "null";
            }
        }
        else
        {
            //use_code=stall_code.at(i);
            //return false;
            return stall_code.at(i);
        }
    }
    //return back;
    return "null";
}

bool datebase::writeCarOutLog(QSqlDatabase *db,QString pic_outname,QString fee,ticketCheck_t *checkInfo)
{
    QSqlQuery query(*db);
    bool ret;
    QString str;
    if(checkInfo->car_type=="固定车")
    {
    str=QObject::tr("update record_in_out set update_time='%1', out_time='%2',out_user='%3',stall_time='%4',status='%5',out_image='%6',receivable='%7',actual_money='%8',discount='%9',car_type='%10' where pass_no='%11'").arg(checkInfo->update_time).arg(checkInfo->endTime).arg(checkInfo->user).arg(checkInfo->parked_time).arg(QString("已出")).arg(pic_outname).arg(checkInfo->receivable).arg(checkInfo->actual_money).arg(checkInfo->discount).arg(checkInfo->car_type).arg(checkInfo->pass_num);
    }else{
    str=QObject::tr("update record_in_out set update_time='%1', out_time='%2',out_user='%3',stall_time='%4',status='%5',out_image='%6',receivable='%7',actual_money='%8',discount='%9' where pass_no='%10'").arg(checkInfo->update_time).arg(checkInfo->endTime).arg(checkInfo->user).arg(checkInfo->parked_time).arg(QString("已出")).arg(pic_outname).arg(checkInfo->receivable).arg(checkInfo->actual_money).arg(checkInfo->discount).arg(checkInfo->pass_num);
    }
    qDebug()<<"writeCarOutLog"<<str;
    query.clear();
    query.prepare(str);
    ret=query.exec();
    if(ret){
        //qDebug()<<"out ture";
        checkInfo->pass_num.clear();
        return true;
    }
    else{
        query.clear();
        query.prepare("insert into record_in_out(pass_no,out_time,out_door,part_time,status,out_image,fee,out_time_var,created_at_var,flag) values(?,?,?,?,?,?,?,?,?,?)");
        query.bindValue(0,checkInfo->pass_num);
        query.bindValue(1,checkInfo->endTime);
        query.bindValue(2,checkInfo->pass_num);
        query.bindValue(3,sysInfo->galleryName);
        query.bindValue(4,"0");
        query.bindValue(5,QString("已出"));
        query.bindValue(6,pic_outname);
        query.bindValue(7,fee);
        query.bindValue(8,checkInfo->endTime);
        query.bindValue(9,checkInfo->endTime);
        query.bindValue(10,0);
        ret=query.exec();
        if(ret)
        {
            qDebug()<<"固定车无入场记录："<<checkInfo->pass_num;
            return true;
        }
        else{
            qDebug()<<"out fasle"<<query.lastError().text();
            return false;
        }
    }
    usleep(500);
}

bool datebase::updateCarOutLog(QSqlDatabase *db,QString vehicle_no)
{
    QSqlQuery query(*db);
    QSqlQuery update_card(*db);
    query.clear();
    update_card.clear();
    query.prepare(QObject::tr("select status from record_in_out where car_no='%1'  and status='%2'").arg(vehicle_no).arg("已入"));
    query.exec();
    int ret=query.next();
    if(ret==1)
    {
        if(sysInfo->first_down==1)//防潜回开启
        {
            return true;
        }else{
            QDateTime time = QDateTime::currentDateTime();
            QString crnttime = time.toString("yyyy-MM-dd hh:mm:ss");
            update_card.exec(QObject::tr("update record_in_out set update_time='%1',out_time='%2',status='%3',modife_flage=1 where car_no='%4' and status='%5'").arg(crnttime).arg(crnttime).arg("跟出").arg(vehicle_no).arg(QString("已入")));
            update_card.next();
            return false;
        }
    }
    query.clear();
    return false;
}

/*bool datebase::updateCarOutLog(QSqlDatabase *db,QString carNum,QString cartype,QString stall_code)
//{
//    QSqlQuery query(*db);
//    //QSqlQuery update_card(*db);
//    query.clear();
//    //update_card.clear();
//    query.prepare(QObject::tr("select status from record_in_out where car_no='%1'  and status='%2'").arg(carNum).arg("已入"));
//    query.exec();
//    int ret=query.next();
//    if(ret==1)
//    {
//        if(sysInfo->serverIpBei=="passFlag")
//        {
//        return true;
//        }else{
//        update_card.exec(QObject::tr("update record_in_out set status='%1' where car_no='%2' and status='%3'").arg(QString("跟出")).arg(vehicle_no).arg(QString("已入")));
//        update_card.clear();
//        return false;
//        }
//    }
//    query.clear();
//    return false;
}*/

bool datebase::CarOutInfo(QSqlDatabase *db, QString vehicle_flag, QString vehicle_no,ticketCheck_t *checkInfo)
{
    QDateTime time = QDateTime::currentDateTime();
    QString cuttime = time.toString("yyyy-MM-dd hh:mm:ss");
    QString str,car_no;
    int ret;
    QSqlQuery query(*db);
    query.clear();
    if(vehicle_no=="无牌")
    {
        str=QObject::tr("select pass_no,in_time,status,car_type from record_in_out where car_no='%1' and status='%2' and in_image='%3' order by in_time desc limit 1").arg(vehicle_no).arg("已入").arg(checkInfo->inImage);
        query.prepare(str);
    }else{
        if(vehicle_flag=="1")  //校验车号所有位数
        {
        str=QObject::tr("select pass_no,in_time,status,car_type from record_in_out where car_no='%1' and status='%2' order by in_time desc limit 1").arg(vehicle_no).arg("已入");
        query.prepare(str);
    }
    else if(vehicle_flag=="0")  //校验车号后6位
    {
        car_no='%'+vehicle_no.right(5)+'%';
        str=QObject::tr("select pass_no,in_time,status,car_type from record_in_out where car_no like :inputName and status='%1' order by in_time desc limit 1").arg("已入");
        query.prepare(str);
        query.bindValue(":inputName",car_no);
    }
    }
    query.exec();
    ret = query.next();
    qDebug()<<"CarOutInfo"<<str<<ret;
    if(ret==1)
    {
        QString pass_num,in_time,status,car_type;
        pass_num=query.value(0).toString();
        in_time=query.value(1).toString().left(19);
        status=query.value(2).toString();
        car_type=query.value(3).toString();
        qDebug()<<"ret==1"<<pass_num<<in_time<<status;
        if(status=="已入")
        {
            qDebug()<<"status::"<<status;
        }else{
            qDebug()<<"status::"<<status;
        }
        checkInfo->beginTime=in_time;
        checkInfo->endTime=cuttime;
        int park_minu=get_park_minute(in_time,checkInfo->endTime);//停车秒数
        QString message=get_park_time(park_minu); //计算停车时长

        checkInfo->parkTime=message;//停车时长
        checkInfo->parked_time=QString::number(park_minu/60);
        checkInfo->car_type=car_type;
        qDebug()<<"Message"<<message<<ret<<checkInfo->parked_time<<park_minu/60;
        if(car_type=="临时车")
        {
            checkInfo->dayAuto=1;
            //get_park_money(db,in_time,cuttime,1);
        }else if(car_type=="固定车")
        {
          checkInfo->receivable=QString::number(0);
          checkInfo->actual_money=QString::number(0);
          checkInfo->discount=QString::number(0);
        }else{
            checkInfo->dayAuto=2;
            //checkInfo->money=get_park_money(db,in_time,cuttime,2);
        }
        checkInfo->pass_num=pass_num;
        return true;
    }else{
        return false;
    }
}

bool datebase::CarOutInfo(QSqlDatabase *db,  QString vehicle_no,ticketCheck_t *checkInfo)
{

    QDateTime time = QDateTime::currentDateTime();
    QString cuttime = time.toString("yyyy-MM-dd hh:mm:ss");
    QString str;
    int ret;
    QSqlQuery query(*db);
    query.clear();
    str=QObject::tr("select pass_no,in_time_var,status,card_type from record_in_out where car_no='%1' and status='%2' order by in_time_var desc limit 1").arg(vehicle_no).arg("已入");
    query.prepare(str);
    query.exec();
    ret = query.next();
    qDebug()<<"CarOutInfo"<<str<<ret;
    if(ret==1)
    {
        QString pass_num,in_time_var,status,car_type;
        pass_num=query.value(0).toString();
        in_time_var=query.value(1).toString().left(19);
        status=query.value(2).toString();
        car_type=query.value(3).toString();
        qDebug()<<"ret==1"<<pass_num<<in_time_var<<status;

        int park_minu=get_park_minute(in_time_var,cuttime);
        QString message=get_park_time(park_minu); //计算停车时长
        qDebug()<<"message"<<message<<ret;
        checkInfo->beginTime=in_time_var;
        checkInfo->endTime=cuttime;
        checkInfo->parkTime=message;
        checkInfo->car_type=car_type;
        checkInfo->pass_num=pass_num;
        return true;
    }else{
        return false;
    }
    usleep(500);
}

bool datebase::writeSqliteCar(QSqlDatabase *db,ticketCheck_t *checkInfo)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare("insert into record_stall_vehicle(car_no,created_at,start_date,stop_date,person_name,person_tel,person_address,status,fee_type,car_type) values (?,?,?,?,?,?,?,?,?,?)");
    query.bindValue(0,checkInfo->car_no);
    query.bindValue(1,checkInfo->update_time);
    query.bindValue(2,checkInfo->beginTime);
    query.bindValue(3,checkInfo->endTime);
    query.bindValue(4,checkInfo->person_name);
    query.bindValue(5,checkInfo->person_tel);
    query.bindValue(6,checkInfo->person_address);
    query.bindValue(7,"正常");
    query.bindValue(8,checkInfo->fee_type);
    query.bindValue(9,checkInfo->car_type);
    int  guet = query.exec();
    if(guet==1)
    {
        qDebug()<<"insert success"<<checkInfo->beginTime<<checkInfo->endTime;
        return true;
    }else{
        qDebug()<<"insert false";
        return false;
    }
}

bool datebase::writeSqliteCarweb(QSqlDatabase *db,webcar_down *webCarInfo)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare("insert into record_stall_vehicle(id,car_no,updated_at,start_date,stop_date,person_name,person_tel,person_address,status) values (?,?,?,?,?,?,?,?,?)");
    query.bindValue(0,webCarInfo->id_car);
    query.bindValue(1,webCarInfo->car_no);
    query.bindValue(2,webCarInfo->updated_at);
    query.bindValue(3,webCarInfo->start_date);
    query.bindValue(4,webCarInfo->stop_date);
    query.bindValue(5,webCarInfo->person_name);
    query.bindValue(6,webCarInfo->person_tel);
    query.bindValue(7,webCarInfo->person_address);
    query.bindValue(8,webCarInfo->status);
    int  guet = query.exec();
    if(guet==1)
    {
        usleep(10000);
        qDebug()<<"insert success"<<webCarInfo->begin_time<<webCarInfo->end_time;
        return true;
    }else{
        qDebug()<<"insert false";
        return false;
    }
}

void datebase::databse_close()
{
    QString name;
    sqlite_db.close();
    name = sqlite_db.connectionName();
    //超出作用域，隐含对象QSqlDatabase::database()被删除。
    QSqlDatabase::removeDatabase(name);
}
int datebase::get_park_minute(QString begin_time, QString end_time)
{
    QDateTime startDateT=QDateTime::fromString(begin_time,"yyyy-MM-dd hh:mm:ss");
    QDateTime endDateT=QDateTime::fromString(end_time,"yyyy-MM-dd hh:mm:ss");
    //qDebug()<<"get_park_minute"<<begin_time<<end_time;
    //qDebug()<<"get_park_minute"<<startDateT<<startDateT;
    int sec=startDateT.secsTo(endDateT);//停车时间(秒)
    return sec;
}
QString datebase::get_park_time(int sec)  //计算停车时长秒转换为时分秒
{
    QString park_time;
    QStringList list;
    div_t temp,temp1;
    temp=div(sec,3600); //计算小时
    temp1=div(temp.rem,60);
    if(temp1.rem>0)
    {
        temp1.quot++;
        if(temp1.quot==60)
        {
            temp1.quot=0;
            temp.quot++;
        }
    }
    if(temp1.quot>=10)
    {
        park_time=QString::number(temp.quot).append(".")+QString::number(temp1.quot);
    }
    else if(temp1.quot<10)
    {
        park_time=QString::number(temp.quot).append(".0")+QString::number(temp1.quot);
    }
    list.clear();
    list=park_time.split('.');
    QString hour,min;
    hour=list[0];
    min=list[1];
    QString message;
    if(hour>"0")
    {
        message=hour.append("小时").append(min).append("分");
    }
    else if(hour=="0")
    {
        message=min.append("分");
    }
    return message;
}
double datebase::get_park_money(QSqlDatabase *db,QString begin_time, QString end_time,QString car_type)  //计算停车费用
{
  double total;
  total=0.0;
  int park_ti;
  long park_sec;
  QSqlQuery query(*db);
  QString str;
  QDateTime startDateT=QDateTime::fromString(begin_time,"yyyy-MM-dd hh:mm:ss");
  QDateTime endDateT=QDateTime::fromString(end_time,"yyyy-MM-dd hh:mm:ss");
  park_sec=startDateT.secsTo(endDateT);
  qDebug()<<"get_park_money--park_sec--"<<park_sec<<car_type;

  if(car_type=="探亲车"){
      park_ti=sysInfo->FreeTime2.toInt();
  }else{
      park_ti=sysInfo->FreeTime.toInt();
  }

  if(park_sec <= park_ti*60){
      qDebug()<<"return "<<park_sec<<park_ti*60;
    return total;
  }else{
      if(sysInfo->LoseFreeTime==1)//核减免费时长)
      {
          park_sec= park_sec-park_ti*60;
      }
      qDebug()<<"get_park_money--park_sec ="<<sysInfo->LoseFreeTime<<park_sec;
  }
  div_t day;
  day=div(park_sec,86400);
  if(day.quot>0) //整天
  {
      /****计算停车整天的金额****/
      query.prepare(QObject::tr("select money from money where 1440 between  parked_min_time and parked_max_time and car_type_name='%1'").arg(car_type));
      query.exec();
      query.next();
      int DayMax= query.value(0).toInt();
      total=DayMax*day.quot;
      if(sysInfo->AddDayTime==0)
      {
          total= DayMax;
          return total;
      }
      qDebug()<<"大于一天"<<total;
  }
  else if(day.quot==0){
      total=0;
  }
  /***不足一天停车费用**/
  div_t min;
  min=div((day.rem),60);
  if(min.rem>0)
  {
      min.quot++;
  }
  str=QObject::tr("select money from money where  parked_min_time <='%1' and parked_max_time >='%2' and car_type_name='%3'").arg(QString::number(min.quot)).arg(QString::number(min.quot)).arg(car_type);
  query.clear();
  query.prepare(str);
  query.exec();
  query.next();
  total=total+query.value(0).toDouble();
  qDebug()<<"get_park_money"<<str<<query.value(0).toDouble()<<total;
  return total;
}

QString datebase::select_carTime(QSqlDatabase *db)
{
    QSqlQuery query(*db);
    QString pass_no;
    query.clear();
    query.prepare(QObject::tr("select id from record_stall_vehicle order by updated_at desc limit 1"));
    query.exec();
    int ret = query.next();
    if(ret==1) //固定车
    {
        pass_no=query.value(0).toString();
    }else{

        pass_no="null";
    }
    qDebug()<<"car_time"<<pass_no;
    return pass_no;
}

QString datebase::select_updateTime(QSqlDatabase *db)
{   //返回固定车更新时间戳
    QSqlQuery query(*db);
    QString lasttime;
    query.clear();
    query.prepare(QObject::tr("select updated_at from record_stall_vehicle order by updated_at desc limit 1"));
    query.exec();
    int ret = query.next();
    if(ret==1)
    {
        lasttime=query.value(0).toString();
        if(lasttime.isEmpty())
        {
        lasttime="1970-01-01 01:00:00";
        }
    }else{
        lasttime="1970-01-01 01:00:00";
    }
    qDebug()<<"datebase::select_updateTime() lasttime"<<lasttime<<ret<<lasttime.isEmpty();
    return lasttime;
}
bool datebase::update_lasttime_record(QSqlDatabase *db,QString lasttime)
{
    QSqlQuery query(*db);
    query.clear();
    //haschanged
    query.prepare(QString("update DownTime set handler_in_out_record_download_time='%1'").arg(lasttime));
    query.exec();
    if(query.next()){
        qDebug()<<"update update_lasttime_record  ok"<<lasttime;
    }else
        qDebug()<<query.lastError().text();
}

//返回通行记录更新时间戳
QString datebase::select_updateTime_record(QSqlDatabase *db)
{//haschanged
    QSqlQuery query(*db);
    QString lasttime;
    query.clear();
    query.prepare(QString("select handler_in_out_record_download_time from DownTime"));
    query.exec();

    if(query.next()){
        lasttime=query.value(0).toString();
    }else{
        lasttime="1970-01-01 01:00:00";
    }
    return lasttime;
}


QString datebase::select_updateTime_carbind(QSqlDatabase *db)
{   //返回通行记录更新时间戳
  QSqlQuery query(*db);
  QString lasttime;
  query.clear();
  //haschanged
  query.prepare(QObject::tr("select handler_down_record_stall_vehicle_time from DownTime"));
  query.exec();
  if(query.next()){
      lasttime=query.value(0).toString();
  }else{
      lasttime="1970-01-01 01:00:00";
  }
  qDebug()<<"select_updateTime_carbind last time "<<lasttime;
  return lasttime;
}

QString datebase::select_updateTime_carstall(QSqlDatabase *db)
{
   QSqlQuery query(*db);
   QString lasttime;
   query.clear();
   //haschanged
   query.prepare(QObject::tr("select handler_down_info_stall_time from DownTime"));
   query.exec();
   if(query.next()){
       lasttime=query.value(0).toString();
   }else{
       lasttime="1970-01-01 01:00:00";
   }
   qDebug()<<"=============select_updateTime_carstall last time"<<lasttime;
   return lasttime;
}


//入场查看和carum绑定的车位并返回
QStringList datebase::select_stall_code(QSqlDatabase *db,QString carNum)
{
    QSqlQuery query(*db);
    QStringList stall_code;
    QString str;
    int i=0;
    query.clear();
    //qDebug()<<"carNum"<<carNum;
    str=QObject::tr("select stall_code from car_bind where car_no='%1'").arg(carNum);
    query.prepare(str);
    query.exec();
    while(query.next())
    {
       stall_code.insert(i,query.value(0).toString());
       qDebug()<<carNum<<"的车位"<<query.value(0).toString();
       i++;
    }
    qDebug()<<"stall_code.count()"<<stall_code.count();
    return stall_code;
}
QString datebase::check_pass_no(QSqlDatabase *db,web_record_in_out *recordInfo)
{
    QSqlQuery query(*db);
    query.clear();

    query.prepare(QObject::tr("select status from record_in_out where pass_no='%1'").arg(recordInfo->pass_no));
    query.exec();
    if(query.next())
    {
        return query.value(0).toString();
        qDebug()<<query.value(0).toString();
    }
    else
        return "null";
}
bool datebase::check_car_bind(QSqlDatabase *db,web_car_bind *webCarbind)
{
    QSqlQuery query(*db);
    QString str;
    query.clear();
    str=QObject::tr("select status from car_bind where code='%1'").arg(webCarbind->code);
    query.prepare(str);
    query.exec();
    bool res=query.next();
    qDebug()<<"str="<<str<<res;
    if(res)
    {
        return true;
    }
    else return false;
}

bool datebase::check_car_bind(QSqlDatabase *db,QString carNum)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare(QObject::tr("select * from car_bind where car_no='%1'").arg(carNum));
    return query.exec();
    //return query.next();
}
bool datebase::delete_car_bind(QSqlDatabase *db,QString car_no,QString stall_code)
{
    qDebug()<<"delete_car_bind--";
    QSqlQuery query(*db);
    query.clear();
    QString str;
    str=QObject::tr("delete from car_bind where car_no='%1' and stall_code='%2'").arg(car_no).arg(stall_code);
    qDebug()<<"str"<<str;
    query.prepare(str);
    return query.exec();
    //    int res=query.next();
    //    qDebug()<<"str"<<str<<"res"<<res;
    //    if(res==1)
    //    {
    //        return true;
    //    }
    //   else return false;
}
bool datebase::delete_car_bind(QSqlDatabase *db,QString car_no)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare(QObject::tr("delete from car_bind where car_no='%1'").arg(car_no));
    query.exec();
    return query.next();
}

bool datebase::update_car_bind(QSqlDatabase *db,web_car_bind *webCarbind)
{
    QSqlQuery query(*db);
    query.clear();
    QString str;
    str=QObject::tr("update car_bind set begin_date='%1',end_date='%2',created_at='%3',updated_at='%4' where car_no='%5',and stall_code='%6'")
            .arg(webCarbind->begin_date).arg(webCarbind->end_date).arg(webCarbind->created_at).arg(webCarbind->updated_at).arg(webCarbind->car_no).arg(webCarbind->stall_code);
    query.prepare(str);
    int res;
    res=query.next();
    qDebug()<<"str"<<str<<"res"<<res;
    if(res==1)
    {
        return true;
    }
    else return false;

}

bool datebase::check_car_stall(QSqlDatabase *db,web_car_stall *webCarstall)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare(QObject::tr("select * from vehicle_seat where stall_code='%1'").arg(webCarstall->stall_code));
    query.exec();
    return query.next();
}
bool datebase::delete_car_stall(QSqlDatabase *db,web_car_stall *webCarstall)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare(QObject::tr("delete from vehicle_seat where stall_code='%1'").arg(webCarstall->stall_code));
    query.exec();
    return query.next();
}

bool datebase::insert_record_in_out(QSqlDatabase *db,web_record_in_out *recordInfo)
{
    //haschanged
    QSqlQuery query(*db);
    query.clear();
    QString str;
    str=QString("insert into record_in_out(pass_no,car_type,car_no,in_time,in_user,in_image,stall,out_time,out_user,out_image,status,tatal_receiv,actual_money,update_time,stall_time,discount,receivable,pay_way,modife_flage) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    query.prepare(str);
    query.bindValue(0,recordInfo->pass_no);      query.bindValue(1,recordInfo->car_type);
    query.bindValue(2,recordInfo->car_no);       query.bindValue(3,recordInfo->in_time);
    query.bindValue(4,recordInfo->in_operator);  query.bindValue(5,recordInfo->in_image);
    query.bindValue(6,recordInfo->stall_code);   query.bindValue(7,recordInfo->out_time);
    query.bindValue(8,recordInfo->out_operator); query.bindValue(9,recordInfo->out_image);
    query.bindValue(10,recordInfo->status);      query.bindValue(11,recordInfo->fee);
    query.bindValue(12,recordInfo->fact_fee);    query.bindValue(13,recordInfo->updated_at);
    query.bindValue(14,recordInfo->parked_time); query.bindValue(15,recordInfo->promote_fee);
    query.bindValue(16,recordInfo->single_fee);  query.bindValue(17,recordInfo->paytype);
    query.bindValue(18,recordInfo->flag);
    qDebug()<<"insert_record_in_out:"<<str;
    bool guet = query.exec();
    if(guet)
    {
        usleep(1000);
        qDebug()<<"datebase::insert_record_in_out success"<<recordInfo->in_time<<recordInfo->out_time;
        return true;
    }else{
        qDebug()<<"datebase::insert_record_in_out false";
        return false;
    }
}



bool datebase::update_record_in_out(QSqlDatabase *db,web_record_in_out *recordInfo)
{//haschanged
   QDateTime time = QDateTime::currentDateTime();
   QString crnttime = time.toString("yyyy-MM-dd hh:mm:ss");
   QSqlQuery query(*db);
   query.clear();
   QString str=QString("update record_in_out set status='%1',out_time='%2',out_user='%3',out_image='%4',fee='%5',fact_fee='%6',stall_time='%7',update_time='%8',modife_flage='%9' where pass_no='%10'")
           .arg(recordInfo->status).arg(recordInfo->out_time).arg(recordInfo->out_operator).arg(recordInfo->out_image)
           .arg(recordInfo->fee).arg(recordInfo->fact_fee).arg(recordInfo->parked_time).arg(crnttime).arg(recordInfo->flag).arg(recordInfo->pass_no);
   query.prepare(str);
   qDebug()<<str;
   if(query.exec())
   {
       usleep(10000);
       qDebug()<<"datebase::update_record_in_out success"<<recordInfo->status<<recordInfo->out_time<<recordInfo->out_operator;
       return true;
   }else{
       qDebug()<<"datebase::update_record_in_out false";
       return false;
   }
}


bool datebase::insert_car_stall(QSqlDatabase *db,web_car_stall *webCarstall)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare("insert into vehicle_seat(area_code,print_code,stall_code,status,created_at,updated_at) values (?,?,?,?,?,?)");
    qDebug()<<"insert _car_stall";
    query.bindValue(0,webCarstall->area_code);
    query.bindValue(1,webCarstall->print_code);
    query.bindValue(2,webCarstall->stall_code);
    query.bindValue(3,webCarstall->status);
    query.bindValue(4,webCarstall->created_at);
    query.bindValue(5,webCarstall->updated_at);
    bool  guet = query.exec();
    if(guet)
    {
        usleep(10000);
        return true;
    }else{
        return false;
    }
}

bool datebase::insert_car_bind(QSqlDatabase *db,web_car_bind *webCarbind)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare("insert into car_bind(code,car_no,stall_code,status,begin_date,end_date,created_at,updated_at) values(?,?,?,?,?,?,?,?)");
    query.bindValue(0,webCarbind->code);
    query.bindValue(1,webCarbind->car_no);
    query.bindValue(2,webCarbind->stall_code);
    query.bindValue(3,webCarbind->status);
    query.bindValue(4,webCarbind->begin_date);
    query.bindValue(5,webCarbind->end_date);
    query.bindValue(6,webCarbind->created_at);
    query.bindValue(7,webCarbind->updated_at);
    bool  guet = query.exec();
    usleep(10000);
    if(guet)
    {
        qDebug()<<"insert car_bind success";
    }else{
        qDebug()<<"insert car bind infomation false";
        return false;
    }
}

void datebase::car_delete(QSqlDatabase *db,QString car_no)
{
    QSqlQuery query(*db);
    query.clear();
    query.exec(QObject::tr("delete from record_stall_vehicle  where car_no='%1'").arg(car_no));
    qDebug()<<"void datebase::car_delete delete from record_stall_vehicle  where car_no="<<car_no;
}

void datebase::cardate_delete(QSqlDatabase *db)
{
    QSqlQuery query(*db);
    QString dele_day;
    dele_day=QDateTime::currentDateTime().addDays(-1).toString("yyyy-MM-dd");
    query.clear();
    int m=query.exec(QObject::tr("delete from record_stall_vehicle  where stop_date <='%1'").arg(dele_day));
    if(m)
    {
        qDebug()<<"void datebase::cardate_delete delete from record_stall_vehicle  where stop_date <="<<dele_day;
    }
}
void datebase::inOut_delete(QSqlDatabase *db,QString pass_no)
{
    QSqlQuery query(*db);
    query.clear();
    query.exec(QObject::tr("delete from record_in_out where pass_no='%1'").arg(pass_no));
    qDebug()<<"delete sqlite success"<<pass_no;
}
void datebase::inOutIc_delete(QSqlDatabase *db,QString card_no)
{
    QSqlQuery query(*db);
    //query=new QSqlQuery(*db);
    query.clear();
    query.exec(QObject::tr("delete from record_in_out where car_no='%1'").arg(card_no));
    query.clear();
    qDebug()<<"void datebase::inOutIc_delete delete from record_in_out where car_no="<<card_no;
}
void datebase::inOutDay_delete(QSqlDatabase *db)
{
    QSqlQuery query(*db);
    //query=new QSqlQuery(*db);
    QString dele_day=QDateTime::currentDateTime().addDays(-30).toString("yyyy-MM-dd hh:mm:ss");
    query.clear();
    int m=query.exec(QObject::tr("delete from record_in_out where created_at <='%1' and card_type='%2'").arg(dele_day).arg("固定车"));
    query.clear();
    if(m)
    {
        qDebug()<<"void datebase::inOutDay_delete delete from record_in_out where created_at <='%1' and card_type=固定车"<<dele_day;
    }
}

bool datebase::update_record_flag(QSqlDatabase *db,web_record_in_out info_record)
{
    QSqlQuery query(*db);
    query.clear();
    query.prepare(QObject::tr("update record_in_out set flag=1 where pass_no='%1'").arg(info_record.pass_no));
    if(query.exec())
    {
        qDebug()<<"datebase::update_record_flag success"<<info_record.pass_no;
        return true;
    }
    else return false;
}

web_record_in_out datebase::select_inout_record(QSqlDatabase *db,web_record_in_out info_record)
{
   //haschanged
   QSqlQuery query(*db);
   query.clear();
   QString str;//
   str=QString("select pass_no,car_type,car_no,in_time,in_user,in_image,stall,out_time,out_user,out_image,status,tatal_receiv,receivable,actual_money,discount,update_time,stall_time,pay_way from record_in_out where modife_flage=0 order by update_time desc limit 1");
   qDebug()<<str;
   query.prepare(str);
   query.exec();

   int ret = query.next();
   if(ret==1)
   {
       info_record.pass_no=query.value(0).toString();//
       info_record.car_type=query.value(1).toString();//
       info_record.car_no=query.value(2).toString();//
       info_record.in_time=query.value(3).toString();//
       info_record.in_operator=query.value(4).toString();//
       info_record.in_image=query.value(5).toString();//
       info_record.stall_code=query.value(6).toString();//
       info_record.out_time=query.value(7).toString();//
       info_record.out_operator=query.value(8).toString();//
       info_record.out_image=query.value(9).toString();//
       info_record.status=query.value(10).toString();//
       info_record.fee=query.value(11).toString();//
       info_record.single_fee=query.value(12).toFloat();
       info_record.fact_fee=query.value(13).toString();
       info_record.promote_fee=query.value(14).toFloat();
       info_record.created_at=query.value(15).toString();
       info_record.updated_controller_sn=query.value(16).toString();
       info_record.parked_time=query.value(17).toString();
       info_record.paytype=query.value(18).toInt();
   }
   else
   {
       info_record.car_no=" ";
       qDebug()<<"web_record_in_out datebase::select_inout_record return false";
   }
   return info_record;
}

int datebase::send_in_out_record(QSqlDatabase *db,ticketCheck_t *checkInfo)
{
    QSqlQuery query(*db);
    QString pass_no,car_type,car_no,in_time,in_door,status,out_time,out_door,park_time,created_at,in_image,out_image,in_operator,out_operator;
    QString stall_code,fee,fact_fee;
    query.clear();
    query.prepare(QObject::tr("select pass_no,card_type,car_no,in_time_var,in_door,in_image,stall_code,out_time_var,out_door,out_image,status,fee,fact_fee,created_at,part_time from record_in_out order by created_at desc limit 1"));
    query.exec();
    int ret = query.next();
    if(ret==1)
    {
        pass_no=query.value(0).toString();
        car_type=query.value(1).toString();
        car_no=query.value(2).toString();
        in_time=query.value(3).toString();
        in_door=query.value(4).toString();
        in_image=query.value(5).toString();
        stall_code=query.value(6).toString();
        out_time=query.value(7).toString();
        out_door=query.value(8).toString();
        out_image=query.value(9).toString();
        status=query.value(10).toString();
        fee=query.value(11).toString();
        fact_fee=query.value(12).toString();
        created_at=query.value(13).toString();
        park_time=query.value(14).toString();

        cJSON *root;
        QByteArray by1,by2,by3,by4,by5,by6,by7,by8,by9,by0,by10,by11,by12,by13,by14;
        char *out,*root_passn,*root_car_type,*root_car_no,*root_in_time,*root_in_door,*root_status,*root_out_time,*root_out_door,*root_park_time,*root_cretime,*root_inimage,*root_outimage;
        char *root_fact_fee,*root_fee,*root_stall_code;
        by0=pass_no.toLocal8Bit();
        root_passn=by0.data();

        by1=car_type.toLocal8Bit();
        root_car_type=by1.data();

        by2=car_no.toLocal8Bit();
        root_car_no=by2.data();

        by3=in_time.toLocal8Bit();
        root_in_time=by3.data();

        by4=in_door.toLocal8Bit();
        root_in_door=by4.data();

        by5=in_image.toLocal8Bit();
        root_inimage=by5.data();

        by6=stall_code.toLocal8Bit();
        root_stall_code=by6.data();

        by7=out_time.toLocal8Bit();
        root_out_time=by7.data();

        by8=out_door.toLocal8Bit();
        root_out_door=by8.data();

        by9=out_image.toLocal8Bit();
        root_outimage=by9.data();

        by10=status.toLocal8Bit();
        root_status=by10.data();

        by11=fee.toLocal8Bit();
        root_fee=by11.data();

        by12=fact_fee.toLocal8Bit();
        root_fact_fee=by12.data();

        by13=created_at.toLocal8Bit();
        root_cretime=by13.data();

        by14=park_time.toLocal8Bit();
        root_park_time=by14.data();

        root=cJSON_CreateObject();
        cJSON_AddStringToObject(root, "pass_no",root_passn);
        cJSON_AddStringToObject(root, "card_type", root_car_type);
        cJSON_AddStringToObject(root, "car_no", root_car_no);
        cJSON_AddStringToObject(root,"in_time",root_in_time);
        cJSON_AddStringToObject(root, "in_operator",root_in_door);//in_operator
        cJSON_AddStringToObject(root, "in_image",root_inimage);
        //stall_code    占用车位号，车位主键
        cJSON_AddStringToObject(root, "stall_code",root_stall_code);
        cJSON_AddStringToObject(root, "out_time", root_out_time);
        cJSON_AddStringToObject(root, "out_operator",root_out_door);//out_operator
        cJSON_AddStringToObject(root, "out_image", root_outimage);//out_image
        cJSON_AddStringToObject(root, "status", root_status);
        cJSON_AddStringToObject(root, "fee", root_fee);
        cJSON_AddStringToObject(root, "fact_fee", root_fact_fee);
        cJSON_AddStringToObject(root, "created_at", root_cretime);
        cJSON_AddStringToObject(root, "parted_time",root_park_time);


        out=cJSON_Print(root);
        QString send;
        send=QString(out);
        checkInfo->ticketReturn=send.simplified();
        if(status=="已入")
        {
            checkInfo->flag="IN";
        }else if(status=="已出"){
            checkInfo->flag="OUT";
        }else{
            checkInfo->flag="OUT";
        }
        return ret;
    }else{
        return ret;
    }
}

bool datebase::inOutimage_selcet(QSqlDatabase *db,ticketCheck_t *checkInfo)
{
    QString image_name;
    if(checkInfo->status=="已出")
    {
     image_name=checkInfo->outImage;
    }else{
     image_name=checkInfo->inImage;
    }
    QSqlQuery query(*db);
    query.clear();
    query.prepare(QObject::tr("select code,dir from temp_vehicle where code='%1' order by create_time desc limit 1").arg(image_name));
    query.exec();
    int ret = query.next();
    if(ret==1)
    {
        //zhangjie
        checkInfo->ticketReturn=query.value(0).toString();
        checkInfo->dirflag=query.value(1).toString();
        qDebug()<<"datebase::inOutimage_selcet return true"<<checkInfo->ticketReturn<<checkInfo->dirflag;
        return true;
    }else{
        checkInfo->ticketReturn="null";
        checkInfo->dirflag="null";
        qDebug()<<"datebase::inOutimage_selcetselect return false"<<checkInfo->ticketReturn<<checkInfo->dirflag;
        return false;
    }
}

bool datebase::inOutimage_insert(QSqlDatabase *db,QString picname,QString car_no,QString dir_path)
{
    QDateTime time = QDateTime::currentDateTime();
    QString cuttime = time.toString("yyyy-MM-dd hh:mm:ss");
    QSqlQuery query(*db);
    //query=new QSqlQuery(*db);
    query.clear();
    query.prepare("insert into temp_vehicle(code,car_no,create_time,dir) values (?,?,?,?)");
    query.bindValue(0,picname);
    query.bindValue(1,car_no);
    query.bindValue(2,cuttime);
    query.bindValue(3,dir_path);
    int  guet = query.exec();
    if(guet==1)  //抓拍开门
    {
        qDebug()<<"inOutimage_insert return ture";
        return true;
    }else{
        qDebug()<<"inOutimage_insert return fasle";
        return false;
    }
}

bool datebase::inOutimage_move(QSqlDatabase *db,QString picname)
{
    QSqlQuery query(*db);
    query.clear();
    query.exec(QObject::tr("delete from temp_vehicle where code='%1'").arg(picname));
    qDebug()<<"bool datebase::inOutimage_move delete from temp_vehicle where code="<<picname;
}

QString datebase::hand_InImage(QSqlDatabase *db,QString picname)
{
    QString pic_flag;
    QSqlQuery query(*db);
    //query=new QSqlQuery(*db);
    query.clear();
    query.prepare(QObject::tr("select dir from temp_vehicle where code='%1' limit 1").arg(picname));
    query.exec();
    int ret = query.next();
    if(ret==1) //固定车
    {
        pic_flag=query.value(0).toString();
        return pic_flag;
    }else{
        pic_flag="null";
        return pic_flag;
    }
}

bool datebase::SqliteDbCheck(QSqlDatabase *db,ticketCheck_t *checkInfo)
{
    QSqlQuery query_s(*db);
    QString identy_name,end_time;
    //query_s = new QSqlQuery(*db);
    int ret;
    query_s.prepare(QObject::tr("select name,end_date from consumer where identify_no='%1' and status='%2'").arg(checkInfo->ic_sn).arg("正常"));
    query_s.exec();
    ret=query_s.next();
    if(ret == 0 )
    {
        qDebug()<<"ic card no person information";
        QString ticket_name="无效卡";
        checkInfo->ticketReturn=ticket_name;
        return false;
    }
    else if(ret == 1)  //人员卡信息正常
    {
        QDateTime time = QDateTime::currentDateTime();
        QString cuttime = time.toString("yyyy-MM-dd");
        identy_name=query_s.value(0).toString();
        end_time=query_s.value(1).toString();
        qDebug()<<"end_time"<<end_time;
        if(cuttime <= end_time)
        {
            QString ticket_name="请通行";
            checkInfo->ticketReturn=ticket_name;
            qDebug()<<"ic card qing tong";
            return true;
        }else{
            qDebug()<<"ic card no time";
            QString ticket_name="不在有效期";
            checkInfo->ticketReturn=ticket_name;
            return false;
        }
    }
}
bool datebase::inOutDir_move(QSqlDatabase *db,QString dirname)
{
    QSqlQuery *query;
    query=new QSqlQuery(*db);
    query->clear();
    query->exec(QObject::tr("delete from temp_vehicle where dir='%1'").arg(dirname));
    qDebug()<<"bool datebase::inOutDir_move delete from temp_vehicle where dir="<<dirname;
    return true;
}

/**************张超START**********************/

QString datebase::userNameCheck(QSqlDatabase *db)
{
    QSqlQuery query_s(*db);
    QString name;
    int ret;
    query_s.prepare(QObject::tr("select password from user where login='admin'"));
    query_s.exec();
    ret=query_s.next();
    if(ret == 0 )
    {
        name.clear();
    }else{
        name=query_s.value(0).toString();
    }
    return name;
}
bool datebase::userNameUpdate(QSqlDatabase *db,QString passwd)
{
    QSqlQuery update_card(*db);
    update_card.clear();
    update_card.exec(QObject::tr("update user set password='%1' where login='admin'").arg(passwd));
    update_card.clear();
    qDebug()<<"bool datebase::userNameUpdate update user set password='%1' where login=admin"<<passwd;
    return true;
}

bool datebase::setFeeData(QSqlDatabase *db,QString codename,float banFee,float yiFee)
{
    int parked_min_time,park_max_time;
    bool res;
    QSqlQuery query(*db);
    query.clear();
    query.prepare(QObject::tr("delete from money where car_type_name='%1'").arg(codename));
    query.exec();
    res=true;
    float money;
    for(int i=1;i<49;i++)
    {
        if(i==1)
        {
            parked_min_time=0;
            money=banFee;
        }else{
            parked_min_time=(i-1)*30+1;
            if(i%2)
            {
                money=(i/2)*yiFee+banFee;
            }else{
                money =(i/2)*yiFee;
            }
        }
        park_max_time=i*30;
        query.clear();
        query.prepare("insert into money(parked_min_time,parked_max_time,money,created_at,car_type_name,car_type_code) values(?,?,?,?,?,?)");
        query.bindValue(0,parked_min_time);
        query.bindValue(1,park_max_time);
        query.bindValue(2,money);
        query.bindValue(3,QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        query.bindValue(4,codename);
        query.bindValue(5,QString::number(1));
        int  guet = query.exec();
        qDebug()<<parked_min_time<<" "<<park_max_time<<" "<<money<<" ";
        if(guet==1)
        {
            qDebug()<<"insert money success";
        }else{
         res=false;
        }
    }
    return res;
}

bool datebase::setUserData(QSqlDatabase *db,QString name,QString pass,QString type)
{
    bool res;
    QSqlQuery query(*db);
    query.clear();
    query.prepare("insert into user(userName,password,type) values(?,?,?)");
    query.bindValue(0,name);
    query.bindValue(1,pass);
    query.bindValue(2,type);
    res = query.exec();
    return res;
}


bool datebase::updateFeeData(QSqlDatabase *db,QString id,QString codename,QString fee)
{
    QSqlQuery query(*db);
    bool res;
    QString str;
    str=QString("update money set money='%1' where id='%2' and car_type_name='%3'").arg(fee).arg(id).arg(codename);
    query.clear();
    res=query.exec(str);
    qDebug()<<"update money ="<<str;
    return res;
}



QString datebase::selectUserData(QSqlDatabase *db,QString name)
{
    QSqlQuery query(*db);
    QString data,pass,type;
    data="";
    bool res;
    query.clear();
    query.prepare(QObject::tr("select password,type from user where userName='%1'").arg(name));
    query.exec();
    res=query.next();
    if(res)
    {
    pass= query.value(0).toString();
    type= query.value(1).toString();
    data=QString("%1-%2").arg(pass).arg(type);
    }
    query.clear();
    qDebug()<<"bool datebase::selectUserData"<<pass;
    return data;
}

bool datebase::insertUserData(QSqlDatabase *db,user_info *info)
{
    bool res;
    QSqlQuery query(*db);
    query.clear();
    query.prepare("insert into user(userName,password,type) values(?,?,?)");
    query.bindValue(0,info->login);
    query.bindValue(1,info->password);
    query.bindValue(2,info->user_type);
    res=query.exec();
    qDebug()<<"bool datebase::insertUserData"<<res;
    return res;
}

bool datebase::updateUserData(QSqlDatabase *db,int flag,QString userName,QString pass,QString type)
{

    QSqlQuery query(*db);
    bool res;
    QString str;
    str=QString("update user set password='%1',type='%2',userName='%3' where id=%4").arg(pass).arg(type).arg(userName).arg(flag);
    query.clear();
    res=query.exec(str);
    qDebug()<<"update user ="<<str;
    return res;
}

bool datebase::deleteUserData(QSqlDatabase *db,QString userName)
{
    QSqlQuery query(*db);
    query.clear();
    query.exec(QObject::tr("delete from user where userName='%1'").arg(userName));
    qDebug()<<"delete user success";
}

QString datebase::FindSeatData(QSqlDatabase *db,QString car_no)
{
    QString stall;
    stall="";
    QSqlQuery query(*db);
    bool res;
    QString str;
    str=QString("select stall_code from car_bind where car_no=%1").arg(car_no);
    query.clear();
    query.exec(str);
    if(query.next())
    {
    stall=query.value(0).toString();
    }
    qDebug()<<"update user ="<<str;
    return stall;
}

void datebase::DeleteStall_vehicle(QSqlDatabase *db)
{
    QSqlQuery query(*db);
    query.clear();
    query.exec(QObject::tr("delete from record_stall_vehicle"));
    qDebug()<<"delete record_stall_vehicle success";
}
/**************张超END**********************/

QList<car_record_info> datebase::select_record_info(QSqlDatabase *db,QString car_no,QString car_type,QString min_time,QString max_time)
{
    QList<car_record_info> record_info_list;
    QString str;
    QSqlQuery query(*db);
    if(car_type=="固定车"){
        str=QString("select car_no,car_type,in_time,out_time,modife_flage from record_in_out where car_no like '%%1%' and car_type in('月租车','年租车','内部车') and in_time>='%2' and in_time<='%3'").arg(car_no).arg(min_time).arg(max_time);
    }else if(car_type=="内部车"){
        str=QString("select car_no,car_type,in_time,out_time,modife_flage from record_in_out where car_no like '%%1%' and in_time>='%2' and in_time<='%3' and car_type='内部车'").arg(car_no).arg(min_time).arg(max_time);
    }else{//临时车
        str=QString("select car_no,car_type,in_time,out_time,modife_flage from record_in_out where car_no like '%%1%' and in_time>='%2' and in_time<='%3' and car_type='临时车'").arg(car_no).arg(min_time).arg(max_time);
    }
   // qDebug()<<str;
    query.prepare(str);
    query.exec();
    for(int i=0;query.next();i++){
        car_record_info record_info;
        record_info.car_no=query.value(0).toString();
        record_info.car_type=query.value(1).toString();
        record_info.in_time=query.value(2).toString();
        record_info.out_time=query.value(3).toString();
        record_info.modife_flag=query.value(4).toInt();
        record_info_list.append(record_info);
    }
    return record_info_list;
}
QList<car_stall_info> datebase::select_vehicle_info(QSqlDatabase *db,QString carNum)
{
    QList<car_stall_info> stall_info_list;
    QString str;
    QSqlQuery query(*db);
    str=QString("select car_no,car_type,codeId,person_name,person_tel,start_date,stop_date from record_stall_vehicle where car_no like '%%1%'").arg(carNum);
    query.prepare(str);
    query.exec();
    for(int i=0;query.next();i++){
        car_stall_info stall_info;
        stall_info.car_no=query.value(0).toString();
        stall_info.car_type=query.value(1).toString();
        stall_info.code_Id=query.value(2).toString();
        stall_info.person_name=query.value(3).toString();
        stall_info.person_tel=query.value(4).toString();
        stall_info.start_date=query.value(5).toString();
        stall_info.stop_date=query.value(6).toString();
        stall_info_list.append(stall_info);
    }
    return stall_info_list;
}
QList<car_record_info> datebase::select_Fee_info(QSqlDatabase *db,QString car_no,QString Operator,QString min_time,QString max_time)
{
    QList<car_record_info> record_info_list;
    QString str;
    QSqlQuery query(*db);
    str=QString("select car_no,car_type,in_time ,out_time,stall_time, receivable,actual_money,discount,pay_way from record_in_out where receivable>0 and car_no like '%%1%' and in_user='%2' and out_time<='%3' and out_time>='%4'").arg(car_no).arg(Operator).arg(max_time).arg(min_time);
    qDebug()<<str;
    query.prepare(str);
    query.exec();
    for(int i=0;query.next();i++){
        car_record_info record_info;
        record_info.car_no=query.value(0).toString();
        record_info.car_type=query.value(1).toString();
        record_info.in_time=query.value(2).toString();
        record_info.out_time=query.value(3).toString();
        record_info.stall_time=query.value(4).toInt();
        record_info.receivable=query.value(5).toFloat();
        record_info.actual_money=query.value(6).toFloat();
        record_info.discount=query.value(7).toFloat();
        record_info.pay_way=query.value(8).toInt();
        record_info_list.append(record_info);
    }
    return record_info_list;
}
car_record_info datebase::select_detail_record_Fee_info(QSqlDatabase *db,QString car_no,QString car_type,QString intime,QString outtime)
{
    car_record_info record_info;
    QString str;
    QSqlQuery query(*db);
    str=QString("select stall_time, receivable,actual_money,discount,pay_way from record_in_out where car_no='%1' and car_type='%2' and in_time='%3'").arg(car_no).arg(car_type).arg(intime);
    qDebug()<<str;
    query.prepare(str);
    query.exec();
    for(int i=0;query.next();i++){
        record_info.stall_time=query.value(0).toInt();
        record_info.receivable=query.value(1).toFloat();
        record_info.actual_money=query.value(2).toFloat();
        record_info.discount=query.value(3).toFloat();
        record_info.pay_way=query.value(4).toInt();
        return record_info;
    }
}

QStringList datebase::select_user(QSqlDatabase *db)
{
    QString str;
    QStringList userlist;
    QSqlQuery query(*db);
    str=QString("select userName from user");
    qDebug()<<str;
    query.prepare(str);
    query.exec();
    for(int i=0;query.next();i++){
        userlist.append(query.value(0).toString());
    }
    return userlist;
}

QStringList datebase::select_detail_record_info(QSqlDatabase *db,QString car_no,QString car_type,QString intime,QString outtime)
{
    QStringList detail_info_list;
    QSqlQuery query(*db);
    QString in_operator,out_operator,status,stall_code,in_image,out_image;
    QString str=QString("select in_user,out_user,status,stall,in_image,out_image from record_in_out where car_no='%1' and car_type='%2' and in_time='%3'").arg(car_no).arg(car_type).arg(intime);
    qDebug()<<str;
    query.prepare(str);
    query.exec();
    if(query.next()){
        in_operator=query.value(0).toString();
        out_operator=query.value(1).toString();
        status=query.value(2).toString();
        stall_code=query.value(3).toString();
        in_image=query.value(4).toString();
        out_image=query.value(5).toString();
        detail_info_list.insert(0,in_operator);
        detail_info_list.insert(1,out_operator);
        detail_info_list.insert(2,status);
        detail_info_list.insert(3,stall_code);
        detail_info_list.insert(4,in_image);
        detail_info_list.insert(5,out_image);

    }
    return detail_info_list;
}
/*********张超**********/
void datebase::deleteSeat(QSqlDatabase *db)
{
    QSqlQuery query(*db);
    query.clear();
    query.exec(QObject::tr("delete from vehicle_seat"));
    qDebug()<<"delete sqlite vehicle_seat";
}

/*******杰哥********/
QList<car_in_find> datebase::select_in_car(QSqlDatabase *db,int flag,QString car_no,QString car_type,int min_time,int max_time)
{
    QString edtime=QDateTime::currentDateTime().addSecs(-min_time*60*60).toString("yyyy-MM-dd hh:mm");
    QString bgtime=QDateTime::currentDateTime().addSecs(-max_time*60*60).toString("yyyy-MM-dd hh:mm");
    QString str;
    QSqlQuery query(*db);
    QList<car_in_find> car_in_find_list;
    if(flag==0){
        //查找全部无牌车  时间无效
        str=QString("select car_no,in_time,in_image from record_in_out where  car_no='无牌' and status='已入' order by in_time DESC").arg(car_no);
    }if(flag==1){//按车类型，时间要求查找
        if(car_type=="临时车"){
            str=QString("select car_no,in_time,in_image from record_in_out where  car_no like '%%1%' and car_type='%2' and in_time<='%3' and in_time>='%4' and status='已入' order by in_time DESC").arg(car_no).arg(car_type).arg(edtime).arg(bgtime);
        }else if(car_type=="全部"){
            str=QString("select car_no,in_time,in_image from record_in_out where  car_no like '%%1%' and in_time<='%2' and in_time>='%3' and status='已入' order by in_time DESC").arg(car_no).arg(edtime).arg(bgtime);
        }else if(car_type=="固定车"){
            str=QString("select car_no,in_time,in_image from record_in_out where  car_no like '%%1%' and car_type in ('月租车','年租车') and in_time<='%2' and in_time>='%3' and status='已入' order by in_time DESC").arg(car_no).arg(edtime).arg(bgtime);
        }else if(car_type=="内部车"){
            str=QString("select car_no,in_time,in_image from record_in_out where  car_no like '%%1%' and car_type='%2' and in_time<='%3' and in_time>='%4' and status='已入' order by in_time DESC").arg(car_no).arg(car_type).arg(edtime).arg(bgtime);
        }
    }else{//查找全部场内车辆,时间无效
        if(car_type=="临时车"){
            str=QString("select car_no,in_time,in_image from record_in_out where  car_no like '%%1%' and car_type='%2' and status='已入' order by in_time DESC").arg(car_no).arg(car_type);
        }else if(car_type=="全部"){
            str=QString("select car_no,in_time,in_image from record_in_out where  car_no like '%%1%' and status='已入' order by in_time DESC").arg(car_no);
        }else if(car_type=="固定车"){
            str=QString("select car_no,in_time,in_image from record_in_out where  car_no like '%%1%' and car_type in ('月租车','年租车') and status='已入' order by in_time DESC").arg(car_no);
        }else if(car_type=="内部车"){
            str=QString("select car_no,in_time,in_image from record_in_out where  car_no like '%%1%' and car_type='%2' and status='已入' order by in_time DESC").arg(car_no).arg(car_type);
        }
    }
    qDebug()<<str;
    query.prepare(str);
    query.exec();
    for(int i=0;query.next();i++){
        car_in_find car_in_info;
        car_in_info.car_no=query.value(0).toString();
        car_in_info.in_time=query.value(1).toString();
        car_in_info.in_image=query.value(2).toString();
        qDebug()<<"car_in_info.car_no"<<car_in_info.car_no<<car_in_info.in_image;
        car_in_find_list.append(car_in_info);
    }
    return car_in_find_list;
}
QString datebase::select_in_pic(QSqlDatabase *db,QString car_no,QString in_time)
{
    QString in_pic;
    QSqlQuery query(*db);
    QString str;
    str=QString("select in_image from record_in_out where car_no='%1' and in_time='%2'").arg(car_no).arg(in_time);
    qDebug()<<str;
    query.clear();
    query.exec(str);
    if(query.next())
    {
    in_pic=query.value(0).toString();
    }
    qDebug()<<"find in_pic:"<<in_pic;
    return in_pic;
}

int datebase::select_seat_count(QSqlDatabase *db)
{
    int seat;
    QSqlQuery query(*db);
    QString str;
    seat=0;
    str=QString("select count(car_no) from record_in_out where status='%1'").arg(QString("已入"));
    qDebug()<<str;
    query.clear();
    query.exec(str);
    if(query.next())
    {
    seat=query.value(0).toInt();
    }
    qDebug()<<"datebase::select_seat_count:"<<seat;
    return seat;
}

//杰哥 begin
QString datebase::select_updateTime_vehicle(QSqlDatabase *db)
{   //返回固定车更新时间戳
    QSqlQuery query(*db);
    QString lasttime;
    query.clear();
    //haschanged
    query.prepare(QString("select handler_down_info_vehicle_time from DownTime"));
    query.exec();

    if(query.next()){
        lasttime=query.value(0).toString();
    }else{
        lasttime="1970-01-01 01:00:00";
    }
    qDebug()<<"datebase::select_updateTime_vehicle() lasttime"<<lasttime;
    return lasttime;
}


bool datebase::update_updateTime_vehicle(QSqlDatabase *db,QString lasttime)
{   //返回固定车更新时间戳
   QSqlQuery query(*db);
   query.clear();
   //haschanged
   query.prepare(QString("update DownTime set handler_down_info_vehicle_time='%1'").arg(lasttime));
   query.exec();
   if(query.next()){
       qDebug()<<"update update_updateTime_vehicle  ok"<<lasttime;
   }else
       qDebug()<<query.lastError().text();
}

bool datebase::updated_car_bind_time(QSqlDatabase *db,QString last_time)
{
  QSqlQuery query(*db);
  query.clear();
  QString str=QString("update DownTime set handler_down_record_stall_vehicle_time='%1'").arg(last_time);
  query.prepare(str);
  qDebug()<<"updated_car_bind_time"<<str;
  query.exec();
  int res=query.next();
  if(res==1)
      return true;
  else
      return false;
}

bool datebase::updated_car_stall_time(QSqlDatabase *db,QString last_time)
{
  QSqlQuery query(*db);
  query.clear();
  QString str=QString("update DownTime set handler_down_info_stall_time='%1'").arg(last_time);
  query.prepare(str);

  query.exec();

  if(query.next()){
      return true;
  }
}

bool datebase::deleteUser(QSqlDatabase *db)
{
  QSqlQuery query(*db);
  query.clear();
  query.prepare("delete from user");
  query.exec();
  query.next();
  return true;
}
